package com.ganee.agora.view

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.ganee.agora.R
import com.ganee.agora.adapter.RepeatListAdapter
import com.ganee.agora.event.RepeatEvent
import com.ganee.agora.model.RepeatListEntity
import com.ganee.agora.viewmodel.RepeatViewModel
import com.ganee.agora.widget.itemDecoration.MyDecoration
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_repeat.*
import kotlinx.android.synthetic.main.toolbar_with_title.*
import org.greenrobot.eventbus.EventBus
@Route(path = Constance.ACTIVITY_URL_REPEAT)
class RepeatActivity : BaseActivity<RepeatViewModel>(),View.OnClickListener {

    @JvmField
    @Autowired(name = "repeatTitle")
    var repeatTitle:String? = null

    private val dataList:MutableList<RepeatListEntity> = mutableListOf()
    private var mAdapter:RepeatListAdapter? = null

    override fun getViewModel(): RepeatViewModel {
        return ViewModelProviders.of(this@RepeatActivity).get(RepeatViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_repeat)
        ARouter.getInstance().inject(this)
        initToolbar()
        initData()
        initViewAndAdapter()
    }

    private fun initToolbar() {
        toolbarCancel.setOnClickListener(this)
        footer_tv_confirm.setOnClickListener(this)
        toolbarTitle.text = "Repeat the event"
    }

    private fun initViewAndAdapter() {
        repeatRecyclerView.setHasFixedSize(true)
        repeatRecyclerView.layoutManager = LinearLayoutManager(this)

        repeatRecyclerView.addItemDecoration(MyDecoration())
        mAdapter = RepeatListAdapter(dataList)
        repeatRecyclerView.addOnItemTouchListener(object : OnItemClickListener() {

            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                val repeatEntity = adapter.getItem(position) as RepeatListEntity
                for(entity in dataList){
                    entity.check = false
                    if (entity.title == repeatEntity.title){
                        entity.check = true
                    }
                }
                repeatTitle = repeatEntity.title
                mAdapter!!.notifyDataSetChanged()
            }

        })
        repeatRecyclerView.adapter = mAdapter
    }

    private fun initData() {
        dataList.add(RepeatListEntity("Does not Repeat",false))
        dataList.add(RepeatListEntity("Every day",false))
        dataList.add(RepeatListEntity("Every week",false))
        dataList.add(RepeatListEntity("Every 2 weeks",false))
        dataList.add(RepeatListEntity("Every month",false))
        dataList.add(RepeatListEntity("Every year",false))

        for(entity in dataList){
            entity.check = false
            if (entity.title == repeatTitle){
                entity.check = true
            }
        }
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.toolbarCancel -> {
                finish()
            }
            R.id.footer_tv_confirm -> {
                EventBus.getDefault().post(RepeatEvent("Repeat",repeatTitle))
                finish()
            }
        }
    }
}
